$(function(){


    var uri_service_getNewClassify="/api/gzd/jkzx/findColumn";
    var uri_service_getNewList="/api/gzd/jkzx/getNewsList";
    var pagesize = 6;


   //缓存分类的分页数据
    var dataCache={
    };


    //查询分类
    function queryNewsClassify(){
        $.QuickRemote.AjaxJson({
            funcName: uri_service_getNewClassify ,
            params: {},
            callback: function (result) {
                //console.log(result);
                showClassify(result);
            }
        });
    }

    //查询分类详情
    function queryNewsList(url,params,elemId){
        $.QuickRemote.AjaxJson({
            funcName: url,
            params: params,
            callback: function (result) {

                //console.log(result);
                //设置缓存信息每个栏目的页数

                dataCache[params.bqid].pageNumber= Math.ceil(result.total/pagesize);
                //设置请求页数
                dataCache[params.bqid].page=params.page;

                var html=showListTemplate(result);

                $(elemId).append(html);

            }
        });
    }

    //显示分类模板和加载分类需要的模板初始化效果
    function showClassify(data){
        var newData={
            data:data
        };
        var tmpClassify = template("templateNewsClassify",newData);
        var tmpListContainer = template("templateNewsListContainer",newData);
        $('#newClassify').html(tmpClassify);
        $('#ul').html(tmpListContainer);
        //设置列表缓存数据
        $.each(data,function(index,d){
            dataCache[d.bqid]={
                load:false,
                page:0
            };
        });

        //初始化所有分类事件和滑动效果
        initSwiper();

        //初始化数据交互
        initDataView();

    };

    //列表模板
    function showListTemplate(data){

        var result = template("templateNewsList",data);

        return result;

    };


    //跳转到详细页
    function goDetail(param){
        var params='nrid='+param;
        $.pageSwitch.goToUrl("jkzx","detail",params);
    }


	//数据交互效果
    function initDataView(){

        //点击分类加载详情
        $('#newClassify').on('click','.list',function(){

            var classify=$(this).attr('valueli');

            var elemId='#id'+classify;

            var index=$(this).index();
            //console.log(index);

            $('.list-more-nav-box').find('li').removeClass('active').eq(index).addClass('active');

            var queryParams = {"bqid":classify,"page":0,"size":pagesize};

            //判断数据是否是第一次加载
            if(dataCache[classify].page===0){
                queryParams.page=1;
                queryNewsList(uri_service_getNewList,queryParams,elemId);

            }else{
                layui.use('flow', function () {
                    var flow = layui.flow;
                    flow.load({
                        elem: elemId,
                        end: " ",
                        isAuto:true,
                        scrollElem:elemId,
                        done: function (page, next) {
                            console.log(page);

                            //大于数据页数
                            if(page> dataCache[classify].pageNumber||page===1||dataCache[classify].pageNumber===0){
                                $(".layui-flow-more").empty();
                                return ;
                            }

                            dataCache[classify].page=dataCache[classify].page+page;
                            queryParams.page=dataCache[classify].page;
                            queryNewsList(uri_service_getNewList,queryParams,elemId);

                        }
                    });
                });

            }
        });

        $('#newClassify').find('.list').eq(0).trigger('click');

        //跳转到文章详情
        $('#ul').on('click','.news-a',function(){
            goDetail($(this).attr('id'));
        });

    };

    //页面动态效果
    function initSwiper(){

        //分类已列表形式显示
        $('#showMoreList').click(function () {
            //alert();
            if($(this).hasClass('up')){
                $(this).removeClass('up').addClass('down');
                $('.list-more-nav-box').hide();
            }else{
                $(this).removeClass('down').addClass('up');
                $('.list-more-nav-box').show();
            }
        });

        //头部点击事件
        $('.list-more-nav-box').on('click', 'li', function () {
            var index = $(this).index();
            $(".find_nav_list li").eq(index).trigger('click');
            $('.list-more-nav-box').hide();
            $('#showMoreList').removeClass('up').addClass('down');
            $(this).addClass('active').siblings().removeClass('active');
        });


        //幻灯片效果
        var tt= new TouchSlider({
            id:'slider1',
            'auto':'-1',
            fx:'ease-out',
            direction:'left',
            speed:600,
            timeout:5000,
            'before':function(index){

            },
            'after':function(index){
                var li = $(".find_nav_list li").eq(index);
                var swiper_state = li.attr("swiper_state");
                if(swiper_state == "click"){
                    li.removeAttr("swiper_state");
                    return;
                }else{
                    li.trigger("click");
                }
                li.removeAttr("swiper_state");

            }
        });

        $(".find_nav_list").css("left",0);
        $(".find_nav_list li").each(function(){
            $(".sideline").css({left:0});
            $(".find_nav_list li").eq(0).addClass("find_nav_cur").siblings().removeClass("find_nav_cur");
        });
        var nav_w=$(".find_nav_list li").first().width();
        var frist = true;
        $(".sideline").width(nav_w);
        $(".find_nav_list li").on('click', function(){

            //queryNewsData(this);
            var index=$(this).index();
            nav_w=$(this).width();
            $(".sideline").stop(true);
            $(".sideline").animate({left:$(this).position().left},300);
            $(".sideline").animate({width:nav_w});
            $(this).addClass("find_nav_cur").siblings().removeClass("find_nav_cur");
            var fn_w = ($(".find_nav").width() - nav_w) / 2;
            var fnl_l;
            var fnl_x = parseInt($(this).position().left);

            if(flb_w >= fl_w){
                fnl_l = 0;
            }else{
                if (fnl_x <= fn_w) {
                    fnl_l = 0;
                } else if (fn_w - fnl_x <= flb_w - fl_w) {
                    fnl_l = flb_w - fl_w;
                } else {
                    fnl_l = fn_w - fnl_x;
                }
            }
            $(".find_nav_list").animate({
                "left" : fnl_l
            }, 300);

            $('.list-more-nav-box').hide();
            $('#showMoreList').removeClass('up').addClass('down');
            $(this).attr({"swiper_state":"click"});
            if(frist){
                $(this).removeAttr("swiper_state");
                frist = false ;
            }
            tt.slide(index);
        });
        var fl_w=$(".find_nav_list").width();
        var flb_w=$(".find_nav_left").width();

        $(".news-list-box").on('touchstart', function (e) {
            flag = true;
            var touch1 = e.originalEvent.targetTouches[0];
            x1 = touch1.pageX;
            y1 = touch1.pageY;
            ty_left = parseInt($(this).css("left"));
        });

        $(".news-list-box").on('touchmove', function (e) {
            flag = false;
            var touch2 = e.originalEvent.targetTouches[0];
            var  x2 = touch2.pageX;
            var  y2 = touch2.pageY;
            if(ty_left + x2 - x1>=0){
                $(this).css("left", 0);
            }else if(ty_left + x2 - x1<=flb_w-fl_w){
                $(this).css("left", flb_w-fl_w);
            }else{
                $(this).css("left", ty_left + x2 - x1);
            }
            if(Math.abs(y2-y1)>0){
                // e.preventDefault();
            }
        });

        /* $(".news-list-box").on('touchend', function (e) {
             if (!flag){
              // e.preventDefault();
             }
         });*/

    }


    queryNewsClassify();


});

	


